#include "client.h"

int cmsgdef(char * buf, train_t * train) {
    int size = strlen(buf);
    int i = 0;
    int j = 0;
    int k = 0;
    char cmd[2][128];
    bzero(cmd, sizeof(cmd));
    while(i < size) {
        if(isspace(buf[i])) {
            i++;
            continue;
        }
        while(i < size &&!isspace(buf[i])) {
            cmd[j][k++] = buf[i++];
        }
        cmd[j++][k] = '\0';
        k = 0;
    }
    
    if(j == 1) {
        if(strcmp("ls", cmd[0]) == 0) {
            return 0;
        } else if(strcmp("pwd", cmd[0]) == 0) {
            return 1;
        } else if(strcmp("exit", cmd[0]) == 0) {
            return 2;
        } else {
            return -1;
        }
    } else {
        strcpy(train->buf, cmd[1]);
        train->length = strlen(train->buf);
        if(strcmp("cd", cmd[0]) == 0) {
            return 3;
        } else if(strcmp("puts", cmd[0]) == 0) {
            return 4;
        } else if(strcmp("gets", cmd[0]) == 0){
            return 5;
        } else if (strcmp("rm", cmd[0]) == 0) {
            return 6;
        } else if(strcmp("mkdir", cmd[0]) == 0) {
            return 7;
        } else {
            return -1;
        }
    }
}

int readmd5(char *filename, char * buf){
    char cmd[128];
    sprintf(cmd, "%s %s","md5sum", filename);
    FILE *fp = popen(cmd, "r");
    if(fp == NULL)
        return -1;
    bzero(buf, 128);
    fread(buf, 1, 128, fp);
    int size = strlen(buf);
    int i = 0;
    while(i < size) {
        if(isspace(buf[i])) {
            i++;
            continue;
        }
        while(i < size && !isspace(buf[i])) {
            i++;
        }
        break;
    }
    buf[i] = '\0';
    pclose(fp);
    return 0;
}

int login(int sockFd) {
    train_t train; 
    bzero(&train,sizeof(train));
    msgprocess(sockFd,&train);
    printf("%s", train.buf);
    int flag;
    while(1) {
        scanf("%d", &flag);
        if(flag != 1 && flag != 2)
        {
            printf("please enter a correct number:");
        }
        else 
            break;
    }
    puts("sendready");
    send(sockFd, &flag, sizeof(int), MSG_NOSIGNAL);
    // sendn(sockFd, &flag, sizeof(int));
    if(flag == 1) {
        int ret = csign_in(sockFd);
        if(-1 == ret)
        {
            printf("密码错误次数过多，连接关闭\n");
            return -1;
        } else {
            printf("登录成功！\n");
            return 0;
        }
    } else {
        int ret = csign_up(sockFd);
        if(-1 == ret) {
            printf("注册失败\n");
           return -1;
        } else {
            printf("注册成功，自动登录！\n");
            return 0;
        }
    }
}

int csign_in(int sockFd) {

}

int csign_up(int sockFd) {
    char buf[128];
    int flag = 1;

    printf("----------------Sign up-----------------\n");
    while(1) {
        printf("usrname:");
        bzero(buf, sizeof(buf));
        scanf("%s", buf);
        sendn(sockFd, buf, strlen(buf));
        int ret = recvn(sockFd, &flag, sizeof(int));
        printf("ret = %d\n", ret);
        printf("flag = %d\n", flag);
        if(flag == 0)
            break;
        else {
            printf("The username is already in use.\n");
        }
    }
    train_t train;
    msgprocess(sockFd, &train); //接收盐值
    printf("%s\n", train.buf);
    while(1) {
        bzero(buf, sizeof(buf));
        printf("password:");
        scanf("%s", buf);
        char pbuf[128] = {0};
        printf("reenter to confirm:");
        scanf("%s", pbuf);
        if(strcmp(buf, pbuf) == 0)
            break;
        else {
            printf("The two inputs do not match, please try again.\n");
        }
    }
    char crpt[128];
    strcpy(crpt,crypt(buf, train.buf));
    printf("len = %ld, %s\n", strlen(crpt), crpt);
    sendn(sockFd, crpt, strlen(crpt));
    
    return 0;
}